package controllers.wechatlab;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lab.libsvm.driver.svm_predict;
import org.lab.libsvm.driver.svm_scale;
import org.lab.libsvm.driver.svm_train;
import org.shirdrn.document.preprocessing.api.constants.ConfigKeys;
import org.shirdrn.document.preprocessing.driver.TestDocumentPreprocessingDriver;
import org.shirdrn.document.preprocessing.driver.TrainDocumentPreprocessingDriver;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

public class TextProcessingController extends WechatBaseController {
	static final String l="0";
	static final String u="1";
	static final String h="0";
	static final String t="0";
	public static final String LABEL = "label.txt";
	public static final String TERMS = "terms.txt";
	public static final String TRAIN = "train.txt";
	public static final String TEST = "test.txt";

	public static final String TRAIN_Scale = "train_scale.txt";
	public static final String TEST_Scale = "test_scale.txt";
	public static final String MODEL = "model.txt";
	public static final String PREDICT="predict.txt";
	public void showTrain(HttpServletRequest req, HttpServletResponse res) throws IOException {
		Properties p = getApplicationPropertis();
		res.getWriter().println(req.getSession().getId() + "\n");
		res.getWriter().println(req.getHeader("Cookie") + "\n");
		res.getWriter().println(p.get(ConfigKeys.DATASET_TRAIN_SVM_VECTOR_FILE));
		res.getWriter().flush();
		getUserDirectory(req);
	}
	public void showTrainClass(HttpServletRequest req, HttpServletResponse res) throws IOException {
		Properties p = getApplicationPropertis();
		// show label
		System.out.println("显示路径1" + p.get(ConfigKeys.DATASET_LABEL_VECTOR_FILE));
		File label = new File((String) p.get(ConfigKeys.DATASET_LABEL_VECTOR_FILE));
		getFileinLocal(label, LABEL, req, res);
	}

	public void showTrainTerms(HttpServletRequest req, HttpServletResponse res) throws IOException {
		Properties p = getApplicationPropertis();
		// show label
		System.out.println("显示路径2" + p.get(ConfigKeys.DATASET_FEATURE_TERM_VECTOR_FILE));
		File terms = new File((String) p.get(ConfigKeys.DATASET_FEATURE_TERM_VECTOR_FILE));
		getFileinLocal(terms, TERMS, req, res);
	}

	public void showTrainFile(HttpServletRequest req, HttpServletResponse res) throws IOException {
		Properties p = getApplicationPropertis();
		// show label
		String trainfiledir = (String) p.get(ConfigKeys.DATASET_TRAIN_SVM_VECTOR_OUTPUT_DIR);
		
		File f = new File(trainfiledir);
		System.out.println("显示路径3" + trainfiledir );
		getFileinLocal(f, TRAIN, req, res);
	}

	
	//->>>--------------------------------------------libsvm--------------------------------------------------------
		public void showTrainScaleFile(HttpServletRequest req, HttpServletResponse res) throws IOException {
			Properties p = getApplicationPropertis();
			String trainfiledir = (String) p.get(ConfigKeys.DATASET_TRAIN_SVM_VECTOR_OUTPUT_DIR);
			String train=trainfiledir+getUserDirectory(req)+System.getProperty("file.separator")+TRAIN;
			String trainScale=trainfiledir+getUserDirectory(req)+System.getProperty("file.separator")+TRAIN_Scale;
			System.out.printf("参数输出scale："+"-l"+l+"-u"+u,"-f"+train +trainScale);
			String argvTrain[]={"-l",l,"-u",u,"-f",trainScale,train};
			svm_scale.main(argvTrain);	
			//show result
			File f = new File(trainfiledir);
			getFileinLocal(f, TRAIN_Scale, req, res);
		}
		
		public void showModelFile(HttpServletRequest req, HttpServletResponse res) throws IOException {
			Properties p = getApplicationPropertis();
			String trainfiledir = (String) p.get(ConfigKeys.DATASET_TRAIN_SVM_VECTOR_OUTPUT_DIR);
			String trainScale=trainfiledir+getUserDirectory(req)+System.getProperty("file.separator")+TRAIN_Scale;
			String model=trainfiledir+getUserDirectory(req)+System.getProperty("file.separator")+MODEL;
			String argvmodel[]={"-h",h,"-t",t,trainScale ,model};
			System.out.printf("参数输出model："+trainScale+model);
			svm_train.main(argvmodel);
			
			//show result
			File f = new File(trainfiledir);
			getFileinLocal(f,MODEL, req, res);
		}
		
		public void showPredictResult(HttpServletRequest req, HttpServletResponse res) throws IOException {
			Properties p = getApplicationPropertis();
			String testfiledir = (String) p.get(ConfigKeys.DATASET_TEST_SVM_VECTOR_OUTPUT_DIR);
			String test=testfiledir+getUserDirectory(req)+System.getProperty("file.separator")+TEST;
			String testScale=testfiledir+getUserDirectory(req)+System.getProperty("file.separator")+TEST_Scale;
			System.out.printf("参数输出scale："+"-l"+l+"-u"+u,"-f"+testScale+test );
			String argvTest[]={"-l",l,"-u",u,"-f",testScale,test };
			svm_scale.main(argvTest);		
		
			String trainfiledir = (String) p.get(ConfigKeys.DATASET_TRAIN_SVM_VECTOR_OUTPUT_DIR);
			String model=trainfiledir+getUserDirectory(req)+System.getProperty("file.separator")+MODEL;				
			String predict=testfiledir+getUserDirectory(req)+System.getProperty("file.separator")+PREDICT;		
			String argvmodel[]={testScale ,model,predict};
			System.out.printf("参数输出predict："+testScale+model+predict);
			svm_predict.main(argvmodel);	
			
			File f = new File(testfiledir);
			getFileinLocal(f,PREDICT, req, res);
		}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public void getFileinLocal(File file, String filename, HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		BufferedReader reader = null;
		file = new File(new File(file, getUserDirectory(req)), filename);
		try {
			res.setCharacterEncoding("utf-8");
			reader = new BufferedReader(new FileReader(file));
			String tempString = "";
			while ((tempString = reader.readLine()) != null) {

				res.getWriter().println(tempString);
				res.getWriter().flush();
			}
			reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			res.getWriter().println("文件读取错误!");
			e.printStackTrace();
		}

	}
	public void testDoc(String uid, WebSocketSession s) throws IOException {
		Properties p = getApplicationPropertis();
		try {
			TestDocumentPreprocessingDriver.startProc(uid, s, p);
		} catch (RuntimeException re) {
			re.printStackTrace();
			s.sendMessage(new TextMessage(re.getMessage()));
			s.close();
		}
	}
	public void trainDoc(String uid, WebSocketSession s) throws IOException {
		Properties p = getApplicationPropertis();
		try {
			TrainDocumentPreprocessingDriver.startProc(uid, s, p);
		} catch (RuntimeException re) {
			re.printStackTrace();
			s.sendMessage(new TextMessage(re.getMessage()));
			s.close();
		}
	}
}
